/*
 *  Copyright (C) 2010 jp
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.boc.botv.web.controller;

import com.boc.botv.common.Database;
import com.boc.botv.common.UserStatus;
import com.boc.botv.model.User;
import com.boc.botv.service.UserManager;
import com.boc.botv.web.validator.UserValidator;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;

/** 
 *
 * @author jp
 */
@Controller
@RequestMapping("/registration")
@SessionAttributes("user")
public class RegistrationController {

    @Autowired
    private UserManager userManager;
    @Autowired
    private UserValidator validator;

    @InitBinder
    public void initBinder(WebDataBinder dataBinder) {
        dataBinder.setDisallowedFields(new String[]{"id"});
        dataBinder.setRequiredFields(
                new String[]{"login", "password", "checkPassword", "email", "title", "firstName", "lastName", "address0", "zipCode", "city", "country", "termsAccepted"});

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(true);
        dataBinder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }

    @RequestMapping(method = RequestMethod.GET)
    public User setUpForm(Model model) {
        model.addAttribute("menu", "home");
        model.addAttribute("path", "");
        model.addAttribute("title", "Formulaire d'inscription");
        model.addAttribute("currentUrl", "/index.htm");
        return new User();
    }

    @RequestMapping(method = RequestMethod.POST)
    public String processForm(User user, BindingResult result, SessionStatus status, Model model) {
        Database.startTransaction(false);
        validator.validate(user, result);
        if (result.hasErrors()) {
            Database.closeTransaction();
            return "registration";
        }
        user.setStatus(UserStatus.disabled);
        userManager.saveUser(user);
        status.setComplete();
        Database.closeTransaction();
        return "redirect:/index.htm";
    }
}
